#!/bin/sh

# 压缩包路径前缀
archivePrefix='https://static0.xesimg.com/dbagent/20210426/'

cd /tmp
version="$2"
archiveName="dbagent-${version}.tar.gz"

# 下载
curl -o "$archiveName" "${archivePrefix}${archiveName}?t=`date +%s`"
# 解压
if [ ! -d '/tmp/dbagent' ]; then
    mkdir -p /tmp/dbagent
fi
tar -zxvf "$archiveName" -C /tmp/dbagent

# 测试环境用测试环境的配置文件
if [ "$1" = "test" ]
then
  cp /tmp/dbagent/configs/config_test.toml /home/www/dbagent/configs/config.toml
else
  cp /tmp/dbagent/configs/config.toml /home/www/dbagent/configs/config.toml
fi

mv /home/www/dbagent/cmd/dbagent /home/www/dbagent/cmd/dbagent-old
cp /tmp/dbagent/cmd/dbagent /home/www/dbagent/cmd/dbagent

# 启动服务
if [ `supervisorctl status dbagent | grep STARTING | wc -l` -eq 0 ]; then
      supervisorctl restart dbagent
fi

echo "done, install successfully"
